package com.farsunset.cim.client.android;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;


/**
 * {@link DelegateCodec} implementation.
 * @author
 */
public class ErrorResponseCodec implements DelegateCodec {

    @Override
    public void encode(ChannelHandlerContext aCtx, Command aMsg, ChannelBuffer aOut) throws Exception {
        ErrorResponse cmd = (ErrorResponse) aMsg;
        Record rec = Record.forWrite();
        rec.addInt(cmd.getId());
        rec.addByte(cmd.getCode().id());
        rec.addString(cmd.getDescription());
        rec.write(aOut);
    }

    @Override
    public void decode(ChannelHandlerContext aCtx, ChannelBuffer aIn, List<Object> aOut) throws Exception {
        Record rec = Record.read(aIn);
        int cmdId = rec.getInt();
        ErrorResponse.Code code = ErrorResponse.Code.forId(rec.getByte());
        String description = rec.getString();
        ErrorResponse cmd = new ErrorResponse(cmdId, code, description);
        aOut.add(cmd);
    }
}
